from durus.file_storage import FileStorage
from durus.connection import Connection
from durus.persistent_dict import PersistentDict
from durus.persistent import PersistentObject

import store

class PersistentData(PersistentDict):
    def __getstate__(self):
        print 'SAVING DATA', self
        return PersistentDict.__getstate__(self)
    
    def __setstate__(self, state):
        print 'LOADING DATA', self, state
        PersistentDict.__setstate__(self, state)

class PersistentComponent(object):
    def __init__(self, component):
        PersistentObject.__init__(self)
        #self.component = component
        self.typename = component.__class__.__name__
        self.data = PersistentDict()#component.data
        for name, value in component.data.items():
            if isinstance(value, Component):
                value = PersistentComponent(value)
            self.data[name] = value
        
    def to_component(self):
        print 'to component'
        type = comp_types.get(self.typename)
        comp = type(**self.data)
        return comp
    
    '''
    def __getstate__(self):
        print 'get state', self, self.data.items()
        state = {}
        for name, value in self.data.items():
            if isinstance(value, Component):
                value = PersistentComponent(value)
            state[name] = value
        return state
    '''

class Data(dict):
    pass
        

class Component(object):
    def __init__(self, **data):
        self.data = PersistentData()
        for name, value in data.items():
            self.data[name] = value
    
    def get_data(self, name):
        data = self.data.get(name)
        if isinstance(data, PersistentComponent):
            data = item.to_component()
            self.set_data(data)
        return data
    
    def set_data(self, name, value):
        self.data[name] = value
    
    def __getstate__(self):
        print 'SAVING COMPONENT', self
        return {'data':self.data}
    
    def __setstate__(self, state):
        print 'LOADING COMPONENT', self
        self.data = state['data']


comp_types = {}
comp_types['Task'] = type("Task", (Component,), {})

comp_types['Task'].__module__ = 'store'
setattr(store, 'Task', comp_types['Task'])



dbpath      = 'testdb.fs'
storage     = FileStorage(dbpath)
connection  = Connection(storage, cache_size=1)
dbroot      = connection.get_root()


def construct():
    comp1 = comp_types['Task'](a=23, b="hello")
    comp2 = comp_types['Task'](ref=comp1, a="foo")
    comp2.b = 10
    comp3 = comp_types['Task'](ref=comp2)
    comp1.data['ref'] = comp3
    dbroot["comp"] = comp1
    connection.commit()
#construct()
print dbroot['comp']
print dbroot['comp'].data['ref']
print dbroot['comp'].data['ref'].data['ref'].data['a']
print dbroot['comp'].data['ref'].data['ref'].data['a']

dbroot['comp'].data['ref'].data['ref'].data['a'] = "new foo"
connection.commit()

print dbroot['comp'].data['ref'].data['ref'].data['a']







